import {reactive, ref} from "vue";
import {getFriendListService} from '@/server/friendService';
import {ElMessage} from 'element-plus'
import {useFriendsStore} from '@/stores/friends.ts';

export function friendListManage() {

    let friendsStore = useFriendsStore();

    //好友列表加载状态
    let loadFriendStatus = ref(false);

    //好友列表
    let friendList = ref([])
    //好友列表对象
    let friendListObj = reactive({});
    getFriendList();

    //加载好友列表
    function getFriendList() {
        if (friendsStore.friendList.length) {
            friendList.value = friendsStore.friendList;
            for (let i in friendList.value) {
                let item = friendList.value[i];
                friendListObj[item.uuid] = item;
            }
            console.log(friendListObj);
            return;
        }

        loadFriendStatus.value = true;
        getFriendListService({}).then(res => {
            loadFriendStatus.value = false;
            let data = res.data;
            if (data.code) {
                ElMessage.warning(data.message);
                return;
            }
            friendList.value = data.data;
            for (let i in friendList.value) {
                let item = friendList.value[i];
                friendListObj[item.uuid] = item;
            }
            console.log(friendListObj);
            friendsStore.setFriendList(data.data);
        })
    }

    return {friendList, friendListObj, loadFriendStatus};
}